const { config } = require('@vue/test-utils')
const Mock = require('mockjs')
const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}


const myUserData = Mock.mock({
  'data|12':[
    {
      'userId|+1':1,
      'userName':"@cword(3)",
      'passWord':123,
      'userSex':'@natural(0, 1)',
      'userImg':"@image('40x40', '#4A7BF7', '用户图片')",
      'delTag':1
    }
  ]

})
module.exports = [
  // user login
  {
    url: '/vue-admin-template/user/login',
    type: 'post',
    response: config => {
      console.log("user login():------config:%o", config.body);
      const {
        username
      } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }
      console.log("----------user login():-----OK!");
      return {
        code: 20000,
        data: token
      }
    }
  },

  //根据id查询用户
  {
    url: '/vue-admin-template/users/detail',
    type: 'get',
    response: config => {
      const params = config.query;

      let retData = undefined


      myUserData.data.forEach(x => {

        if (x.userId == params.id) {
          retData = x;
        }
      });

      return {
        code: 20000,
        data: {
          "user": retData
        }
      }
    }
  },





  // get user info
  {
    url: '/vue-admin-template/user/info\.*',
    type: 'get',
    response: config => {
      const {
        token
      } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  //获取用户列表
  {
    url: '/vue-admin-template/users/list',
    type: 'get',
    response: config => {

      const params = config.query;
      // console.log("-----mock food data: %o",myFoodData.data);
      //console.log("-----用户列表接口，请求参数:%o", params);

      const fId = (undefined == params.userId || params.userId.trim() == "") ? null : params.userId.trim();
      const fName = (undefined == params.userName || params.userName.trim() == "") ? null : params.userName.trim();

      const pageNo = +params.page; // 页码

      const pageSize = +params.limit; // 每页大小

      let retData = [];


      // 处理：非分页的查询参数
      if (fId != null || fName != null) {
        for (let x of myUserData.data) {


          // console.log("-----fId:%d,fName:%s,x:%o,fId==x.foodId:%s",fId,fName,x,(fId==x.foodId)+"");
          // 1.用户只输入：食品编号
          if ((fId != null && fId == x.userId) && (fName == null)) {
            retData.push(x);
          }
          // 2.用户只输入：食品名称
          if ((fId == null) &&
            (fName != null && x.userName.indexOf(fName) != -1)) {
            retData.push(x);
          }
          // 3. 用户输入的是：食品编号 和 食品名称
          if ((fId != null && fId == x.userId) &&
            (fName != null && x.userName.indexOf(fName) != -1)) {
            retData.push(x);
          }

        }
      } else {

        retData = myUserData.data;
        // console.log(retData);
        // console.log(55555);

      }

      // 处理：分页问题 （将查询出来的结果，按分页参数要求展示适当的数据！）
      let startIndex = (pageNo - 1) * pageSize;
      let endIndex = startIndex + pageSize;
      // console.log(`----startIndex:${startIndex} , endIndex:${endIndex}`);
     // console.log('startIndex,'+startIndex);

      retData = retData.slice(startIndex, endIndex);


      return {
        code: 20000,
        data: {
          "data": retData,
          "total": myUserData.length
        }
      }
    }
  },

  //删除用户
  {
    url: '/vue-admin-template/users/delete',
    type: 'post',
    response: config => {
      const params = config.body;

        //console.log('-------');
        //console.log(myUserData.data);
        let userIndex = -1; //用户数组中的食品索引
        myUserData.data.forEach((x,index) => {
          if (x.userId == params.userId) {
            userIndex = index;
          }
        });
        //将该用户信息从用户列表中删除
        myUserData.data.splice(userIndex,1);


      return {
        code: 20000,
        data:"删除用户成功！"
      }
    }
  },

  //新增用户
  {
    url: '/vue-admin-template/users/save',
    type: 'post',
    response: config => {
      config.body.userImg = myUserData.data[0].userImg
      const user = config.body;

      myUserData.data.push(user)



      return {
        code: 20000,
        data: 'success'
      }
    }
  },

  //编辑用户
  {
    url: '/vue-admin-template/users/edit',
    type: 'post',
    response: config => {
      config.body.userImg = myUserData.data[0].userImg
      const user = config.body;
      console.log('0000000000000000000000000000000');
      console.log(user);
      //console.log(myUserData);
      myUserData.data.forEach((x,index) => {
        if (x.userId == user.userId) {

          myUserData.data.splice(index,1)
          myUserData.data.push(user)
        }
      });
      //myUserData.data.push(user)



      return {
        code: 20000,
        data: 'success'
      }
    }
  },





  // user logout
  {
    url: '/vue-admin-template/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
